package com.example.mybatispdemo.store.controller;


import com.example.mybatispdemo.store.entity.SupplierEntity;
import com.example.mybatispdemo.store.entity.SupplierEntity;
import com.example.mybatispdemo.store.service.ISupplierService;
import com.example.mybatispdemo.store.service.impl.SupplierServiceImpl;
import com.example.mybatispdemo.store.vo.ActionResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljp
 * @since 2022-10-31
 */
@Api(tags="营销商管理")
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    final
    ISupplierService supplierService;

    public SupplierController(SupplierServiceImpl supplierService) {
        this.supplierService = supplierService;
    }

    @Transactional
    @ApiOperation("根据id查找供应商")
    @GetMapping("/{id}")
    public ActionResult getSupplier(@PathVariable@ApiParam("供应商id") int id){
        SupplierEntity Supplier = supplierService.getSupplierById(id);
        if(Supplier==null){
            return ActionResult.fail("查找失败");
        }
        return ActionResult.success(Supplier);
    }
    

    @Transactional
    @ApiOperation("添加供应商")
    @PostMapping
    public ActionResult addSupplier(@RequestBody SupplierEntity Supplier){
        if(supplierService.addSupplier(Supplier)){
            return ActionResult.success("创建成功！");
        }else{
            return ActionResult.fail("创建失败！");
        }
    }

    @Transactional
    @ApiOperation("更新供应商信息")
    @PutMapping()
    public ActionResult updateSupplier(@PathVariable int id,@RequestBody SupplierEntity Supplier){
        if(supplierService.updateSupplierById(id,Supplier)){
            return ActionResult.success("更新成功！");
        }else{
            return ActionResult.fail("更新失败！");
        }
    }

    public ActionResult deleteSupplier(@PathVariable int id){
        if(supplierService.deleteSupplierById(id)){
            return ActionResult.success("更新成功！");
        }else{
            return ActionResult.fail("更新失败！");
        }
    }
}

